#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/3/24 8:39
# @Author : 闫峰
# @File    : base_tool.py
import hmac
import time
import hashlib

from common.setting import ensure_path_sep
# from utils.mysqlUtils.db_pool1 import MySQLPool
from utils.mysqlUtils.db_pool import Database
from utils.mysqlUtils.db_utils import DB


# db = DB(ensure_path_sep("\\config\\config.yaml"), "znkd_database")
db = Database(ensure_path_sep("\\config\\config.yaml"), "znkd_database")


def shop_auth(ordersourceid, apiService):
    timest = int(time.time())
    host = "https://partner.shopeemobile.com"
    path = "/api/v2/shop/auth_partner"
    redirect_url = "https://www.baidu.com/"
    partner_id = "2001656"
    tmp = "test...."
    partner_key = "70e6a5191de45204aea83ab722419dbf54ec1f6cb04a7794f57a5f400d01bb41"
    partner_key = partner_key.encode()
    # tmp_base_string = "%s%s%s" % (partner_id, path, timest)
    sql1 = """	SELECT DevIdSellerId,UserTokenSecretAccessKey  FROM sys_ordersource_authorization WHERE CustomerId=1 AND OrderSourceId=%s;"""
    # res1 = db.execute_sql(sql1, (ordersourceid,))
    res1 = db.fetch_all(sql1, (ordersourceid,))
    mb_str = "2001656" + apiService + str(timest) + res1[0]["UserTokenSecretAccessKey"] + res1[0]["DevIdSellerId"]

    mb_str = mb_str.encode()
    sign = hmac.new(partner_key, mb_str, hashlib.sha256).hexdigest()
    ##generate api
    return sign


def shop_auth_shop(ordersourceid, apiService):
    timest = int(time.time())
    host = "https://partner.shopeemobile.com"
    path = "/api/v2/shop/auth_partner"
    redirect_url = "https://www.baidu.com/"
    partner_id = "2001656"
    tmp = "test...."
    partner_key = "70e6a5191de45204aea83ab722419dbf54ec1f6cb04a7794f57a5f400d01bb41"
    partner_key = partner_key.encode()
    # tmp_base_string = "%s%s%s" % (partner_id, path, timest)
    sql1 = """	SELECT CertIdAccessKeyId,UserTokenSecretAccessKey  FROM sys_ordersource_authorization WHERE CustomerId=1 AND OrderSourceId=%s;"""
    # res1 = db.execute_sql(sql1, (ordersourceid,))
    res1 = db.fetch_all(sql1, (ordersourceid,))
    mb_str = "2001656" + apiService + str(timest) + res1[0]["UserTokenSecretAccessKey"] + res1[0]["CertIdAccessKeyId"]

    mb_str = mb_str.encode()
    sign = hmac.new(partner_key, mb_str, hashlib.sha256).hexdigest()
    ##generate api
    return sign
